package com.mastercard.assets.web.struts.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AlertasEditarForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class AlertasEditarRegistrar
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		AlertasEditarForm f = ( AlertasEditarForm ) form ;
		
		// toma datos
		int operation = f.getOperation( ) ;
		int cveAlerta = f.getCveAlerta( ) ;
		String nombre = f.getNombre( ) ;
		String descripcion = f.getDescripcion( ) ;
		String texto = f.getTexto( ) ;
		
		try
		{
			MasterCardDAO.editAlert( cveAlerta , nombre , descripcion , texto ) ;
			
			// en caso de exito, manda al detalle
			user.setNextWorkArea( "/FlujosIndexIn.do" ) ;
		}catch( Exception e )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			user.setMessageError( e.getMessage( ) ) ;
			
			// en caso de exito, manda al detalle
			user.setNextWorkArea( "/AlertasIndexIn.do?operation=3&cveForma=" + cveAlerta ) ;
		}
		
		MasterCardDAO.closeEntityManager( ) ;
		return mapping.findForward( "success" ) ;
		
	}
	
}
